<?php


namespace app\api\model;


use ApiOper;
use app\common\model\BaseModel;
use common;
use app\common\library\IDMan;

class CustomerServiceOrderModel extends BaseModel{

    var $_selectlist = [
        'id' => 'string',
        'city_id' => 'string',
        'problem_type' => 'string',
        'problem_desc' => 'string',
        'problem_imgs' => 'array',
        'customer_id' => 'string',
        'customer_name' => 'string',
        'customer_phone' => 'string',
        'bound_order_type' => 'string',
        'bound_order_id' => 'string',
        'create_time' => 'string',
        'service_staff_id' => 'string',
        'service_staff_name' => 'string',
        'handle_time' => 'string',
        'handle_result' => 'string',
        'order_state' => 'int',
        'submitter_type' => 'int',
        'submitter_id' => 'string',
    ];

    protected $name = 'customer_service_order';
    protected $pk = 'id';

    function add($info) {
        $info = common::type_conver($info, $this->_selectlist);

        $info['id'] = IDMan::city_order_id($info['city_id']);

        $info['comments'] = [];

        self::create($info,true);
        return $info;
    }

    function query_list($cd, $pos, $limit) {
        $sfs = common::array_keys($this->_selectlist);
        $beans = [
        ];
        if (isset($cd['customer_id'])) {
            $beans['customer_id'] = $cd['customer_id'];
        }
        if (isset($cd['order_state'])) {
            $beans['order_state'] = $cd['order_state'];
        }
        if (isset($cd['service_staff_id'])) {
            $beans['service_staff_id'] = $cd['service_staff_id'];
        }


        if (isset($cd['sk'])) {
            if (common::is_phone($cd['sk'])) {
                $beans['customer_phone'] = $cd['sk'];
            }
        }

        $rows = $this->where($beans)->field($sfs)->order('create_time','desc')->limit($pos,$limit)->select();
        return $rows->toArray();
    }

    function query_info($id, $comment = false) {
        if ($comment) {
            $this->_selectlist['comments'] = 'array';
        }
        $sfs = common::array_keys($this->_selectlist);
        $st = "select SQL_CALC_FOUND_ROWS $sfs  from tb_customer_service_order where  id = ?";
        $beans = [
            'id'=>$id,
        ];

        $row = $this->where($beans)->field($sfs)->find();
        return $row;
    }

    function handle($city_id, $id, $handle_result) {
        $data = [
            'handle_result' => $handle_result,
            'order_state' => CUSTOMER_SERVICE_ORDER_STATE_FINISHED,
            'handle_time' => date('Y-m-d H:i:s'),
        ];


        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);
    }

    function confirm($city_id, $id) {
        $data = [
            'service_staff_id' => ApiOper::instance()->userinfo()['id'],
            'service_staff_name' => ApiOper::instance()->userinfo()['name'],
            'order_state' => CUSTOMER_SERVICE_ORDER_STATE_CONFIRM,
            'confirm_time' => date('Y-m-d H:i:s'),
        ];


        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

       self::update($data, $where);
    }

    function append_comment($id, $content) {
        $item = [
            'content' => $content,
            'time' => date('Y-m-d H:i:s'),
            'user_name' => ApiOper::instance()->userinfo()['name'],
            'user_id' => ApiOper::instance()->userinfo()['id'],
            'user_type' => ApiOper::instance()->userinfo()['user_type'],
        ];

        $st = "update tb_customer_service_order set comments = " . hisql::json_append_object_s('comments', $item) . " where  id = ?";
        $beans = [$id];
        hisql::db()->execute($st, $beans);
    }

    function query_list_service($cd) {
        $sfs = implode(",", common::array_keys($this->_selectlist));
        $st = "select SQL_CALC_FOUND_ROWS $sfs  from tb_customer_service_order where  1 = 1";
        $beans = [
        ];

        if (isset($cd['begin_time'])) {
            $st = $st . " and create_time >= ?";
            $beans[] = $cd['begin_time'];
        }

        if (isset($cd['end_time'])) {
            $st = $st . " and create_time <= ?";
            $beans[] = $cd['end_time'];
        }

        $st = $st . " order by create_time desc";

        $result = hisql::db()->query($st, $beans);
        $rows = $result->fetchAll(Phalcon\Db::FETCH_ASSOC);

        $l = [];
        foreach ($rows as $row) {
            $info = common::sql_row($this->_selectlist, $row);
            $l[] = $info;
        }
        return $l;
    }

}
